package ar.com.larreta.colegio.entidades;

import java.util.Iterator;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import ar.com.larreta.entidades.Persona;

@Entity
@Table(name = "responsable")
// Borrado logico
@SQLDelete (sql="UPDATE Responsable SET fechaEliminacion=CURRENT_TIMESTAMP WHERE ID=?")
// Solamente trae los que no fueron borrados logicamente
@Where(clause="fechaEliminacion IS NULL")
public class Responsable extends Persona {

	private String cbu;
	private Set<Alumno> alumnos;
	private Set<Pago> pagos;

	@OneToMany (mappedBy="responsable", fetch= FetchType.LAZY)
	public Set<Pago> getPagos() {
		return pagos;
	}

	public void setPagos(Set<Pago> pagos) {
		this.pagos = pagos;
	}

	@Basic @Column (name="cbu")
	public String getCbu() {
		return cbu;
	}

	public void setCbu(String cbu) {
		this.cbu = cbu;
	}
	
	@OneToMany (mappedBy="responsable", fetch= FetchType.LAZY, cascade=CascadeType.ALL)
	public Set<Alumno> getAlumnos() {
		return alumnos;
	}
	
	public void setAlumnos(Set<Alumno> alumnos) {
		if (alumnos!=null){
			Iterator<Alumno> it = alumnos.iterator();
			while (it.hasNext()) {
				Alumno alumno = (Alumno) it.next();
				alumno.setResponsable(this);
			}
		}
		this.alumnos = alumnos;
	}
	
}
